#include "MainMenu.h"
#include "Personal.h"
#include "ui_Personal.h"
#include <QDateTime>
#include <QMessageBox>
#include <QDebug>
Personal::Personal(QRect screenGeometry, QString phone, QString id,
                   QString name, QString nation, QString sex,
                   QString blood, QString birth, QString illness,
                   QString irritability, QWidget *parent):
QWidget(parent),
ui(new Ui::Personal)
{
    ui->setupUi(this);
    qDebug()<<"nation:"<<nation<<"      irritability:"<<irritability<<"    illness:"<<illness<<endl;
    this->id=id;
    this->name=name;
    this->nation=nation;
    this->sex=sex;
    this->blood=blood;
    if(id!="")
        this->birth=id.mid(6,4)+"/"+id.mid(10,2)+"/"+id.mid(12,2);
    this->illness=illness;
    this->irritability=irritability;
    this->screenGeometry=screenGeometry;
    ui->phone->setText(MainMenu::phone);
    ui->id->setText(this->id);
    ui->name->setText(this->name);
    ui->nation->setText(this->nation);
    this->sex=="男"?ui->sex_nan->setChecked(true):ui->sex_nv->setChecked(true);
    ui->blood->setCurrentText(this->blood);
    ui->birth->setText(this->birth);
    ui->illness->setText(this->illness);
    ui->irritability->setText(this->irritability);
   // this->ui->widget_5->setGeometry(0,40,screenGeometry.width(),600);
}

Personal::~Personal()
{
    delete ui;
}

void Personal::changePersonal(bool r)
{
    if(r==true)
    {
        this->id=ui->id->text();
        this->name=ui->name->text();
        this->nation=ui->nation->text();
        this->sex=this->ui->sex_nv->isChecked()?"女":"男";
        this->blood=ui->blood->currentText();
        this->birth=ui->birth->text();
        this->illness=ui->illness->text();
        this->irritability=ui->irritability->text();
    }
    else
    {
        ui->phone->setText(MainMenu::phone);
        ui->id->setText(this->id);
        ui->name->setText(this->name);
        ui->nation->setText(this->nation);
        this->sex=="男"?ui->sex_nan->setChecked(true):ui->sex_nv->setChecked(true);
        ui->blood->setCurrentText(this->blood);
        ui->birth->setText(this->birth);
        ui->illness->setText(this->illness);
        ui->irritability->setText(this->irritability);
    }
    QMessageBox *box=new QMessageBox;
    box->warning(nullptr,"",r?"修改成功":"修改失败");
    box->exec();
    delete box;

}

void Personal::sendChangeInformation()
{
    QRegExp idCardRegex("^[1-9]\\d{5}(18|19|20|21|22)?\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|[Xx])$");
    bool r=idCardRegex.exactMatch(ui->id->text());
    if(!r)
    {
        QMessageBox *box=new QMessageBox;
        box->warning(nullptr,"","身份证输入错误");
        box->exec();
        delete box;
        return;
    }
    QString str="7E7E;kind:phone;function:05;phone:%1;pass:%2;name:%3;sex:%4;nation:%5;birth:%6;age:%7;id:%8;blood:%9;illness:%10;irritability:%11;";
    str=str.arg(ui->phone->text()).arg(MainMenu::pass).arg(ui->name->text())
            .arg(ui->sex_nv->isChecked()?"女":"男").arg(ui->nation->text())
            .arg(ui->birth->text()).arg(QDate::currentDate().year()-ui->birth->text().mid(0,4).toInt())
            .arg(ui->id->text()).arg(ui->blood->currentText()).arg(ui->illness->text()).arg(ui->irritability->text());
    emit this->toServerSend_signals(str);
}

void Personal::showPersonal(QString phone, QString id, QString name, QString nation, QString sex, QString blood, QString birth, QString illness, QString irritability)
{
    this->id=id;
    this->name=name;
    this->nation=nation;
    this->sex=sex;
    this->blood=blood;
    if(id!="")
        this->birth=id.mid(6,4)+"/"+id.mid(10,2)+"/"+id.mid(12,2);
    this->illness=illness;
    this->irritability=irritability;
    ui->phone->setText(MainMenu::phone);
    ui->id->setText(this->id);
    ui->name->setText(this->name);
    ui->nation->setText(this->nation);
    this->sex=="男"?ui->sex_nan->setChecked(true):ui->sex_nv->setChecked(true);
    ui->blood->setCurrentText(this->blood);
    ui->birth->setText(this->birth);
    ui->illness->setText(this->illness);
    ui->irritability->setText(this->irritability);
    this->show();
}

void Personal::on_save_clicked()
{
    qDebug()<<"ssssssssss"<<endl;
    QString str="7E7E;kind:phone;function:07;phone:%1;pass:%2;";
    emit this->toServerSend_signals(str.arg(MainMenu::phone).arg(MainMenu::pass));
}

void Personal::on_exit_clicked()
{
    qDebug()<<"hhhhhhhhhhhh"<<endl;
    this->hide();
}

void Personal::on_changed_clicked()
{
    if(this->changePass==nullptr)
    {
        this->changePass=new ChangePass(this->screenGeometry);
        connect(this->changePass,&ChangePass::toServerSend_signals,this,&Personal::toServerSend_signals);
    }
    this->changePass->show();
}
